package com.ruoyi.device.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.device.domain.DFaultReport;
import com.ruoyi.fault.domain.FaultReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.device.mapper.DeviceInfoMapper;
import com.ruoyi.device.domain.DeviceInfo;
import com.ruoyi.device.service.IDeviceInfoService;

/**
 * 设备基本信息Service业务层处理
 * 
 * @author silence
 * @date 2025-07-19
 */
@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService 
{
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    /**
     * 查询设备基本信息
     * 
     * @param id 设备基本信息主键
     * @return 设备基本信息
     */
    @Override
    public DeviceInfo selectDeviceInfoById(Long id)
    {
        return deviceInfoMapper.selectDeviceInfoById(id);
    }

    /**
     * 查询设备基本信息列表
     * 
     * @param deviceInfo 设备基本信息
     * @return 设备基本信息
     */
    @Override
    public List<DeviceInfo> selectDeviceInfoList(DeviceInfo deviceInfo)
    {
        return deviceInfoMapper.selectDeviceInfoList(deviceInfo);
    }

    /**
     * 新增设备基本信息
     * 
     * @param deviceInfo 设备基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDeviceInfo(DeviceInfo deviceInfo)
    {
        deviceInfo.setCreateTime(DateUtils.getNowDate());
        int rows = deviceInfoMapper.insertDeviceInfo(deviceInfo);
        insertFaultReport(deviceInfo);
        return rows;
    }

    /**
     * 修改设备基本信息
     * 
     * @param deviceInfo 设备基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDeviceInfo(DeviceInfo deviceInfo)
    {
        deviceInfo.setUpdateTime(DateUtils.getNowDate());
        deviceInfoMapper.deleteFaultReportByDeviceId(deviceInfo.getId());
        insertFaultReport(deviceInfo);
        return deviceInfoMapper.updateDeviceInfo(deviceInfo);
    }

    /**
     * 批量删除设备基本信息
     * 
     * @param ids 需要删除的设备基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeviceInfoByIds(Long[] ids)
    {
        deviceInfoMapper.deleteFaultReportByDeviceIds(ids);
        return deviceInfoMapper.deleteDeviceInfoByIds(ids);
    }

    /**
     * 删除设备基本信息信息
     * 
     * @param id 设备基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeviceInfoById(Long id)
    {
        deviceInfoMapper.deleteFaultReportByDeviceId(id);
        return deviceInfoMapper.deleteDeviceInfoById(id);
    }

    /**
     * 新增故障报修信息
     * 
     * @param deviceInfo 设备基本信息对象
     */
    public void insertFaultReport(DeviceInfo deviceInfo)
    {
        List<DFaultReport> DFaultReportList = deviceInfo.getFaultReportList();
        Long id = deviceInfo.getId();
        if (StringUtils.isNotNull(DFaultReportList))
        {
            List<DFaultReport> list = new ArrayList<DFaultReport>();
            for (DFaultReport DFaultReport : DFaultReportList)
            {
                DFaultReport.setDeviceId(id);
                list.add(DFaultReport);
            }
            if (list.size() > 0)
            {
                // 将DFaultReport转换为FaultReport
                List<FaultReport> faultReports = new ArrayList<>();
                for (DFaultReport dFaultReport : list) {
                    FaultReport faultReport = new FaultReport();
                    // 复制属性值
                    faultReport.setDeviceId(dFaultReport.getDeviceId());
                    faultReport.setFaultTitle(dFaultReport.getFaultTitle());
                    faultReport.setFaultDesc(dFaultReport.getFaultDesc());
                    faultReport.setFaultLevel(dFaultReport.getFaultLevel());
                    faultReport.setReportBy(dFaultReport.getReportBy());
                    faultReport.setReportTime(dFaultReport.getReportTime());
                    faultReport.setStatus(dFaultReport.getStatus());
                    faultReport.setExpectFinishTime(dFaultReport.getExpectFinishTime());
                    faultReports.add(faultReport);
                }
                deviceInfoMapper.batchFaultReport(faultReports);
            }
        }
    }
}
